package zhoug.player.core.utils;

import android.text.TextUtils;

/**
 * @Author: zhoug
 * @Date: 2022/12/16
 * @Description:
 */
public class PlayUrlUtils {
    /**
     * 视频协议
     */
    public static final int TYPE_RTSP = 1;//"RTSP";
    public static final int TYPE_RTMP = 2;// "RTMP";
    public static final int TYPE_FLV = 3;// "FLV";
    public static final int TYPE_HLS = 4;//"HLS";
    public static final int TYPE_LOCAL = 5;//本地视频


    /**
     * 给定的url是远程播放地址
     * @param url
     * @return
     */
    public static boolean isRemotePlayUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        boolean isRemoteUrl = url.startsWith("http://") || url.startsWith("https://");
        if (!isRemoteUrl) {
            int urlType = getUrlType(url);
            isRemoteUrl = urlType != 0 && urlType != TYPE_LOCAL;
        }
        return isRemoteUrl;
    }


    /**
     * 视频格式
     *
     * @param playUrl
     * @return
     */
    public static int getUrlType(String playUrl) {
        if (TextUtils.isEmpty(playUrl)) {
            return 0;
        }
        playUrl = playUrl.toLowerCase();
        if (playUrl.startsWith("rtmp://")) {
            return TYPE_RTMP;
        } else if (playUrl.startsWith("rtsp://")) {
            return TYPE_RTSP;
        } else if (playUrl.startsWith("http://") || playUrl.startsWith("https://")) {
            if (playUrl.contains(".m3u8") || playUrl.contains(".m3u")) {
                return TYPE_HLS;
            } else if (playUrl.contains(".flv")) {
                return TYPE_FLV;
            }
        } else {
            return TYPE_LOCAL;
        }
        return 0;
    }



}
